package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.EnterpriseInfoMapper;
import com.example.demo.base.ArrayCountUtils;
import com.example.demo.base.Result;
import com.example.demo.entity.Commodity;
import com.example.demo.entity.EnterpriseInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/firmInfo")

public class EnterpriseInfoController {
    @Resource
    EnterpriseInfoMapper enterpriseInfoMapper;

    /**
     * 获取数据库全部数据 √
     *
     * @return
     */
    @GetMapping("/all")
    public Result<?> findAll(@RequestParam String tenantId) {
        QueryWrapper<EnterpriseInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenant_id", Integer.valueOf(tenantId));
        return Result.success(enterpriseInfoMapper.selectList(queryWrapper));
    }

    /**
     * 统计地域  √
     *
     * @param tenantId
     * @return
     */
    @GetMapping("/pieChatData")
    public Result<?> pieChatData(@RequestParam String tenantId) {
        QueryWrapper<EnterpriseInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenant_id", Integer.valueOf(tenantId));
        List<EnterpriseInfo> enterpriseInfoInfoList = enterpriseInfoMapper.selectList(queryWrapper);
        String[] res = new String[enterpriseInfoInfoList.size()];
        for (int i = 0; i < enterpriseInfoInfoList.size(); i++) {
            res[i] = enterpriseInfoInfoList.get(i).getFirmAdd().substring(0, 2);
        }
        ArrayCountUtils arrayCountUtils = new ArrayCountUtils();
        List<ArrayCountUtils.Group> groups = arrayCountUtils.find(res);
        return Result.success(groups);
    }

    /**
     * 分页获取数据库数据  √
     *
     * @param pageNum
     * @param pageSize
     * @return
     */

    @GetMapping(value = "/pageAll")
    public Result<?> findPageE(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam String tenantId) {
        LambdaQueryWrapper<EnterpriseInfo> wrapper = Wrappers.<EnterpriseInfo>lambdaQuery();
        wrapper.eq(EnterpriseInfo::getTenantId, Integer.valueOf(tenantId));
        Page<EnterpriseInfo> enterpriseInfoInfoPage = enterpriseInfoMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(enterpriseInfoInfoPage);
    }


    /**
     * 增加数据  √
     *
     * @param firmNum
     * @param firmName
     * @param firmTel
     * @param firmAdd
     * @param firmProduct
     * @param firmProfile
     * @return
     */

    @GetMapping(value = "/add")
    public Result<?> saveE(@RequestParam String firmNum,
                           @RequestParam String firmName,
                           @RequestParam String firmTel,
                           @RequestParam String firmAdd,
                           @RequestParam String firmProduct,
                           @RequestParam String firmProfile,
                           @RequestParam String tenantId) {
        if (firmNum.isEmpty() || firmName.isEmpty() || firmTel.isEmpty() || firmAdd.isEmpty() || firmProduct.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        EnterpriseInfo enterpriseInfo = new EnterpriseInfo(Integer.valueOf(firmNum), firmName, firmTel, firmAdd, firmProduct, firmProfile, Integer.valueOf(tenantId));

        enterpriseInfoMapper.insert(enterpriseInfo);
        return Result.success();
    }

    /**
     * 按firmNum更新数据  √
     *
     * @param firmNum
     * @param firmName
     * @param firmTel
     * @param firmAdd
     * @param firmProduct
     * @param firmProfile
     * @return
     */
    @GetMapping(value = "/update")
    public Result<?> updateE(@RequestParam String firmNum,
                             @RequestParam String firmName,
                             @RequestParam String firmTel,
                             @RequestParam String firmAdd,
                             @RequestParam String firmProduct,
                             @RequestParam String firmProfile,
                             @RequestParam String tenantId) {
        if (firmNum.isEmpty() || firmName.isEmpty() || firmTel.isEmpty() || firmAdd.isEmpty() || firmProduct.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        EnterpriseInfo enterpriseInfo = new EnterpriseInfo(Integer.valueOf(firmNum), firmName, firmTel, firmAdd, firmProduct, firmProfile, Integer.valueOf(tenantId));
        enterpriseInfoMapper.updateByMultiId(enterpriseInfo);
        return Result.success();
    }

    /**
     * 根据frmNum精确查询  √
     *
     * @param frmNum
     * @return
     */
    @GetMapping("/findById")
    public Result<?> findByFrmNum(@RequestParam String frmNum,
                                  @RequestParam String tenantId) {
        EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
        enterpriseInfo.setFirmNum(Integer.valueOf(frmNum));
        enterpriseInfo.setTenantId(Integer.valueOf(tenantId));
        EnterpriseInfo tmp = enterpriseInfoMapper.selectByMultiId(enterpriseInfo);
        List<EnterpriseInfo> enterpriseInfoList = new ArrayList<>();
        if (tmp != null) {
            enterpriseInfoList.add(tmp);
            return Result.success(enterpriseInfoList);
        }
        return Result.error(0, "没有查询到相关数据");


    }

    /**
     * 根据企业名称分页查询  √
     *
     * @param pageNum
     * @param pageSize
     * @param firmName
     * @return
     */

    @GetMapping(value = "/findByFirmName")
    public Result<?> findByFirmName(@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    @RequestParam String firmName,
                                    @RequestParam String tenantId) {
        LambdaQueryWrapper<EnterpriseInfo> wrapper = Wrappers.<EnterpriseInfo>lambdaQuery();
        wrapper.like(EnterpriseInfo::getFirmName, firmName)
                .eq(EnterpriseInfo::getTenantId, tenantId);
        Page<EnterpriseInfo> enterpriseInfoInfoPage = enterpriseInfoMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        if (enterpriseInfoInfoPage.getTotal() == 0) {
            return Result.error(0, "没有查询到相关数据");
        }
        return Result.success(enterpriseInfoInfoPage);
    }

    @GetMapping(value = "/findOne")
    public Result<?> find(@RequestParam String firmName,
                          @RequestParam String id) {
        LambdaQueryWrapper<EnterpriseInfo> wrapper = Wrappers.<EnterpriseInfo>lambdaQuery();
        wrapper.eq(EnterpriseInfo::getFirmName, firmName)
                .eq(EnterpriseInfo::getTenantId, id);
        EnterpriseInfo enterpriseInfo = enterpriseInfoMapper.selectOne(wrapper);
        if (enterpriseInfo == null) {
            return Result.error(0, "没有查询到相关企业数据，请联系管理员");
        }
        return Result.success(enterpriseInfo);
    }

    @DeleteMapping("/delById")
    public Result<?> delete(@RequestParam String firmId,
                            @RequestParam String tenantId) {
        EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
        enterpriseInfo.setFirmNum(Integer.valueOf(firmId));
        enterpriseInfo.setTenantId(Integer.valueOf(tenantId));
        int res = enterpriseInfoMapper.deleteByMultiId(enterpriseInfo);
        if (res > 0)
            return Result.success();
        return Result.error(0, "删除失败");
    }
}
